package com.soup.view;

import com.soup.dao.BookDao;
import com.soup.domain.Book;
import com.soup.domain.Person;
import com.soup.view.component.Context;
import com.soup.view.component.MassageTip;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.List;

public class SearchBookFrame extends Context implements ActionListener, KeyListener {


    public JTextField search = new JTextField(10);
    public JTextField name = new JTextField(10);
    public JTextField author = new JTextField(10);
    public JTextField publish = new JTextField(10);
    public JTextField have = new JTextField(10);
    public JTextField hot = new JTextField(10);

    private JButton left = new JButton("<");
    private JButton right = new JButton(">");

    private BookDao dao = new BookDao();
    private List<Book> cache = null;
    private int indexOfCache = 0;

    public SearchBookFrame(int width, int height) {
        super(width, height);

        search.setBounds(60,0,width-120,40);

        name.setBounds(60,150,width-120,40);
        author.setBounds(60,200,width-120,40);
        publish.setBounds(60,250,width-120,40);
        have.setBounds(60,300,width-120,40);
        hot.setBounds(60,350,width-120,40);

        this.add(search, "搜索", 100);
        this.add(name, "名称", 100);
        this.add(author, "作者", 100);
        this.add(publish, "出版社", 100);
        this.add(have, "库存", 100);
        this.add(hot, "热度", 100);

        left.setBounds(0,(height-40)/2,40,40);
        this.add(left,20);
        right.setBounds(width-40,(height-40)/2,40,40);
        this.add(right,20);

        search.addKeyListener(this);
        left.addActionListener(this);
        right.addActionListener(this);
        search.setText("按照名称搜索，回车确认");
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if (cache != null){
            if (e.getSource() == left){
                if (indexOfCache > 0){
                    indexOfCache --;
                    setValue(cache.get(indexOfCache));
                }else{
                    MassageTip.showMassage(null,"前面再没有了");
                }
            }else if (e.getSource() == right){
                if (indexOfCache < cache.size()-1){
                    indexOfCache ++;
                    setValue(cache.get(indexOfCache));
                }else {
                    MassageTip.showMassage(null,"后面再没有了");
                }
            }
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        if (e.getKeyChar() == KeyEvent.VK_ENTER){
            cache = dao.findByName("%"+search.getText()+"%");
            indexOfCache = 0;
            if (cache != null && cache.size() != 0){
                setValue(cache.get(indexOfCache));
                MassageTip.showMassage(null,"找到"+cache.size()+"个");
            }else {
                MassageTip.showMassage(null,"没有找到");
            }
        }
    }

    private void setValue(Book book) {
        name.setText(book.getName());
        author.setText(book.getAuthor());
        publish.setText(book.getPublish());
        have.setText(book.getHave()+"");
        hot.setText(book.getHot()+"");
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
}
